package hu.arguscab.physic;

/**
 *
 * @author sharp
 */
public final class Vector {
	private float x, y;
	private float length;


	public Vector() {
		x = y = 0;
	}
	
	public Vector(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public Vector(double x, double y) {
		this.x = (float) x;
		this.y = (float) y;
	}

	public Vector(final Vector v) {
		this.x = v.getX();
		this.y = v.getY();
	}

	public Vector(Angle a) {
		setByAngle(a);
	}

	public float getDistanceFrom(final Vector v) {
		float dx = Math.abs( v.getX() - this.x );
		float dy = Math.abs( v.getY() - this.y );
		return Math.max(dx, dy);
	}

	public void set(final Vector v, float len) {
		Vector normal = Vectors.normalize(v);
		Vector uj = Vectors.multiply(normal, len);
		this.x = uj.getX();
		this.y = uj.getY();
	}

	public void checkAndSetMaxLength(float maxLen) {
		if ( this.getLength() > maxLen )
			this.setLength(maxLen);
	}

	public void setLength(float len) {
		float k = (len/this.getLength());
		this.x *= k;
		this.y *= k;
	}

	public void setNull() {
		x = y = 0;
	}

	public boolean isNull() {
		return (x == 0) && (y == 0);
	}

	/**
	 * Létrehoz egy "a" irányú egységvektort
	 * @param a
	 */
	public void setByAngle(Angle a) {
		x = (float) Math.cos((a.get()/180)*Math.PI);
		y = (float) Math.sin((a.get()/180)*Math.PI);
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public Vector add(final Vector v) {
		x += v.getX();
		y += v.getY();
		return this;
	}

	public Vector multiply(final float s) {
		x *= s;
		y *= s;
		return this;
	}

	public Vector divide(final float s) {
		x /= s;
		y /= s;
		return this;
	}

	public float getLength() {
		length = (float)Math.sqrt((double)(x*x+y*y));
		return length;
	}

	@Override
	public String toString() {
		return x + "," + y;
	}

	public boolean equals(final Vector f) {
		return this.x == f.getX() && this.y == f.getY();
	}
}
